package com.domino.server.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;

public class CookieUtil {
	
	public static String toString(Cookie cookie) {
		StringBuilder sb = new StringBuilder();
		sb.append("name:");
		sb.append(cookie.getName());
		sb.append("\n path:");
		sb.append(cookie.getPath());
		sb.append("\n domain:");
		sb.append(cookie.getDomain());
		sb.append("\n value:");
		sb.append(cookie.getValue());
		sb.append("\n version:");
		sb.append(cookie.getVersion());
		sb.append("\n secure:");
		sb.append(cookie.getSecure());
		sb.append("\n maxage:");
		sb.append(cookie.getMaxAge());
		return sb.toString();
	}

	public static String getValue(Cookie cookie, String string) {
		String []values = cookie.getValue().split("&");
		for (int i=0;i<values.length; i++) {
			String []tuple = values[i].split("=");
			if (tuple.length>1 && tuple[0].equals("uid")) {
				return tuple[1];
			}
		}
		return null;
	}

	public static Map<String, String> getMap(Cookie cookie) {
		Map<String, String> res = new HashMap<String, String>();
		String []values = cookie.getValue().split("&");
		for (int i=0;i<values.length; i++) {
			String []tuple = values[i].split("=");
			if (tuple.length>1) {
				res.put(tuple[0], tuple[1]);
			}
		}
		return res;
	}

}
